package com.bawei.flink.demo;

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.text.SimpleDateFormat;
import java.time.Duration;

/**
 *
 * 输入
 * orange
 * 1,1631838600000,orange   30:00
 * 1,1631838602000,orange   30:02
 * 1,1631838610000,orange   30:10
 *
 * green
 * 1,1631838599000,green    29:59
 * 1,1631838600000,green    30:00
 * 1,1631838601000,green    30:01
 * 1,1631838608000,green    30:08
 * 1,1631838611000,green    30:11
 *
 * 输出
 * joinkey = 1,leftdata = 2021-09-17 08:30:00@orange,rightdata = 2021-09-17 08:29:59@green
 * joinkey = 1,leftdata = 2021-09-17 08:30:02@orange,rightdata = 2021-09-17 08:30:00@green
 * joinkey = 1,leftdata = 2021-09-17 08:30:00@orange,rightdata = 2021-09-17 08:30:00@green
 * joinkey = 1,leftdata = 2021-09-17 08:30:02@orange,rightdata = 2021-09-17 08:30:01@green
 * joinkey = 1,leftdata = 2021-09-17 08:30:00@orange,rightdata = 2021-09-17 08:30:01@green
 * joinkey = 1,leftdata = 2021-09-17 08:30:10@orange,rightdata = 2021-09-17 08:30:08@green
 * joinkey = 1,leftdata = 2021-09-17 08:30:10@orange,rightdata = 2021-09-17 08:30:11@green
 *
 *
 */
public class IntervalJoinDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        KeyedStream<Tuple3<String, Long, String>, String> greenStream = env.socketTextStream("cdh1", 9999)
                .map(new MapFunction<String, Tuple3<String, Long, String>>() {
                    @Override
                    public Tuple3<String, Long, String> map(String value) throws Exception {
                        String[] arr = value.split(",");
                        return Tuple3.of(arr[0], Long.valueOf(arr[1]), arr[2]);
                    }
                })
                .assignTimestampsAndWatermarks(WatermarkStrategy
                        .<Tuple3<String, Long, String>>forBoundedOutOfOrderness(Duration.ZERO)
                        .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, Long, String>>() {
                            @Override
                            public long extractTimestamp(Tuple3<String, Long, String> element, long recordTimestamp) {
                                return element.f1;
                            }
                        })).keyBy(new KeySelector<Tuple3<String, Long, String>, String>() {
                    @Override
                    public String getKey(Tuple3<String, Long, String> value) throws Exception {
                        return value.f0;
                    }
                });
        KeyedStream<Tuple3<String, Long, String>, String> orangeStream =
          env.socketTextStream("cdh1", 9998)
          .map(new MapFunction<String, Tuple3<String, Long, String>>() {
            @Override
            public Tuple3<String, Long, String> map(String value) throws Exception {
                String[] arr = value.split(",");
                return Tuple3.of(arr[0], Long.valueOf(arr[1]), arr[2]);
            }
        })
                .assignTimestampsAndWatermarks(WatermarkStrategy
                        .<Tuple3<String, Long, String>>forBoundedOutOfOrderness(Duration.ZERO)
                        .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, Long, String>>() {
                            @Override
                            public long extractTimestamp(Tuple3<String, Long, String> element, long recordTimestamp) {
                                return element.f1;
                            }
                        })).keyBy(new KeySelector<Tuple3<String, Long, String>, String>() {
                    @Override
                    public String getKey(Tuple3<String, Long, String> value) throws Exception {
                        return value.f0;
                    }
                });

        orangeStream
                .intervalJoin(greenStream)
                .between(Time.seconds(-2),Time.seconds(1))
                .process(new ProcessJoinFunction<Tuple3<String, Long, String>, Tuple3<String, Long, String>, String>() {

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    @Override
                    public void processElement(Tuple3<String, Long, String> left, Tuple3<String, Long, String> right, Context ctx, Collector<String> out) throws Exception {
                        String outStr = "joinkey = "+left.f0 + ",leftdata = " + sdf.format(left.f1) + "@" +left.f2 + ",rightdata = " + sdf.format(right.f1) + "@" + right.f2;
                        out.collect(outStr);
                    }
                })
                .print();

        env.execute();

    }

}
